#include "calendar.h"
#include "Arduino.h"
#include "TFT_eSPI.h"
#include "PCA9554.h"
extern TFT_eSPI tft;
extern PCA9554 io; 

extern lv_obj_t *scr_tft;
extern lv_obj_t *scr_eink;
extern lv_obj_t *label_menu;

extern bool eink_scene_ready;
extern bool eink_flash_ready;


static lv_obj_t *scr;
static void menu_close(lv_obj_t *obj, lv_event_t event);
static void event_handler(lv_obj_t * obj, lv_event_t event)
{
    if(event == LV_EVENT_CLICKED) {
        lv_calendar_date_t * date = lv_calendar_get_pressed_date(obj);
        if(date) {
            lv_calendar_set_today_date(obj, date);
            // eink_scene_ready=false;
	        // eink_flash_ready=false;

        }
        eink_scene_ready=false;
        eink_flash_ready=false;
    }
    else if(LV_EVENT_VALUE_CHANGED == event)//当前月份发生更改时
    {
        // eink_scene_ready=false;
        // eink_flash_ready=false;
    }
}
static void btn_back_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(event == LV_EVENT_CLICKED) {
        lv_obj_t * par = lv_obj_get_parent(btn);
        lv_obj_del(par);
         
        lv_obj_set_hidden(label_menu,false);
        eink_scene_ready=false;
        eink_flash_ready=false;
    }
}
void calendar_create(lv_obj_t *obj, lv_event_t event)
{
    if(event == LV_EVENT_CLICKED) 
    {
        
         
        lv_label_set_text( label_menu,"");
        // io.digitalWrite0(3, 0);//   //TFT_CS//   //TFT_CS
        // tft.fillScreen(0xff00);
        // io.digitalWrite0(3, 1);//   //TFT_CS//   //TFT_CS

        // win = lv_win_create(scr_eink,NULL);
        // lv_obj_t *btn = lv_win_add_btn(win,LV_SYMBOL_CLOSE);
        // lv_obj_set_event_cb(btn,menu_close);
        //eink
         //eink
        eink.setTextColor(GxEPD_BLACK);
        eink.setFullWindow();
        eink.firstPage();
        do
        {
            eink.fillScreen(GxEPD_WHITE);        
        }
        while (eink.nextPage());
        
        scr = lv_obj_create(scr_eink,NULL);
        lv_obj_set_size(scr,176,264);

        btn_back_create(scr,btn_back_event_cb);
        // lv_obj_t *btn_back = lv_btn_create(scr,NULL);
        // lv_obj_set_size(btn_back,50,50);
        // lv_obj_set_pos(btn_back,0,0);
        // lv_obj_set_event_cb(btn_back, btn_back_event_cb);
        
        // lv_obj_t *label_back = lv_label_create(btn_back,NULL);
        // lv_label_set_text(label_back,LV_SYMBOL_LEFT);
        // lv_obj_align(label_back,btn_back,LV_ALIGN_CENTER,0,0);


        lv_obj_t *calendar = lv_calendar_create(scr,NULL);
        lv_obj_set_width(calendar,176);
        lv_obj_set_height(calendar,214);
        lv_obj_align(calendar, NULL, LV_ALIGN_IN_TOP_MID, 0, 0);
        lv_obj_set_event_cb(calendar,event_handler);


        static lv_style_t style;
        lv_style_copy(&style,lv_calendar_get_style(calendar,LV_CALENDAR_STYLE_DAY_NAMES));
        style.text.font = &lv_font_roboto_12;
        lv_calendar_set_style(calendar, LV_CALENDAR_STYLE_DAY_NAMES, &style);
        lv_calendar_set_style(calendar, LV_CALENDAR_STYLE_HEADER, &style);
        lv_calendar_set_style(calendar, LV_CALENDAR_STYLE_HEADER_PR, &style);
        lv_calendar_set_style(calendar, LV_CALENDAR_STYLE_BG, &style);

         static lv_style_t style_today;
        lv_style_copy(&style_today,lv_calendar_get_style(calendar,LV_CALENDAR_STYLE_HIGHLIGHTED_DAYS));
        style_today.text.font = &lv_font_roboto_16;
        lv_calendar_set_style(calendar, LV_CALENDAR_STYLE_HIGHLIGHTED_DAYS, &style_today);

        static lv_style_t style_inactive;
        lv_style_copy(&style_inactive,lv_calendar_get_style(calendar,LV_CALENDAR_STYLE_INACTIVE_DAYS));
        style_inactive.text.font = &lv_font_unscii_8;        
        lv_calendar_set_style(calendar, LV_CALENDAR_STYLE_INACTIVE_DAYS, &style_inactive);

         
        DateTime now = rtc.now();
         
        /*Set the today*/
        lv_calendar_date_t today;
        today.year = now.year();
        today.month = now.month();
        today.day = now.day();

        lv_calendar_set_today_date(calendar, &today);
        lv_calendar_set_showed_date(calendar, &today);

        /*Highlight some days*/
        // static lv_calendar_date_t highlihted_days[3];       /*Only it's pointer will be saved so should be static*/
        // highlihted_days[0].year = 2018;
        // highlihted_days[0].month = 10;
        // highlihted_days[0].day = 6;

        // highlihted_days[1].year = 2018;
        // highlihted_days[1].month = 10;
        // highlihted_days[1].day = 11;

        // highlihted_days[2].year = 2018;
        // highlihted_days[2].month = 11;
        // highlihted_days[2].day = 22;

        // lv_calendar_set_highlighted_dates(calendar, highlihted_days, 3);


        eink_flash_ready = false;
        eink_scene_ready = false;
    }
    else if(event == LV_EVENT_VALUE_CHANGED) {
        printf("Toggled\n");
    }
    


}

static void menu_close(lv_obj_t *obj, lv_event_t event)
{
	// lv_obj_del(ta);
	// lv_obj_del(lab);
	// lv_obj_del(btnm1);

	// ta=NULL;
	// lab=NULL;
	// btnm1=NULL;
    if(event == LV_EVENT_RELEASED) {
        lv_obj_t * win = lv_win_get_from_btn(obj);

        lv_obj_del(win);
    }

	lv_obj_set_hidden(label_menu,false);
	eink_scene_ready=false;
	eink_flash_ready=false;

}


